View Javadoc
1   /*
2    * GenericXPathQueryService.java
3    *
4    * Created on December 21, 2004, 1:19 PM
5    */
6   
7   package gov.noaa.eds.xapi.generic.modules;
8   
9   import org.apache.log4j.Logger;
10  import org.apache.xpath.XPathAPI;
11  import org.w3c.dom.Node;
12  import org.w3c.dom.NodeList;
13  import org.xmldb.api.base.Collection;
14  import org.xmldb.api.base.Resource;
15  import org.xmldb.api.base.ResourceSet;
16  import org.xmldb.api.base.XMLDBException;
17  import org.xmldb.api.modules.XMLResource;
18  import org.xmldb.api.modules.XPathQueryService;
19  import gov.noaa.eds.xapi.generic.*;
20  import javax.xml.transform.TransformerException;
21  
22  /*** A slow query service that examines each resource one by one.  It uses
23   * Xalan Xpath implementation {@link http://xml.apache.org/xalan-j}.
24   *
25   * @version $Id: GenericXPathQueryService.java,v 1.3 2004/12/23 23:39:15 mrxtravis Exp $
26   * @author tns
27   */
28  public class GenericXPathQueryService extends GenericConfigurable implements XPathQueryService {
29      
30      private static Logger log = Logger.getLogger(GenericXPathQueryService.class);
31      
32      private static String version = "1.0";
33      private Collection collection = null;
34      
35      /*** Creates a new instance of GenericXPathQueryService */
36      public GenericXPathQueryService() {
37      }
38      
39      /*** Unsupported
40       *@todo Figure out how namespaces fit in and implement this method
41       */
42      public void removeNamespace(String str) {
43          throw new UnsupportedOperationException();
44      }
45      
46      /*** Grabs each Resource from a collection and runs an XPath query on
47       *the DOM.  If one or more nodes match the query, then a resource is added
48       *the the ResourceSet.
49       *@param query The string query to perform on the collection
50       *@return A set of resources which match the query
51       */
52      public ResourceSet query(String query) throws XMLDBException {
53          if (this.collection == null){
54              throw new IllegalStateException("Collection property has not been set.");
55          }
56          if (query == null){
57              throw new NullPointerException("Parameter query can not be null");
58          }
59          
60          //must query all resources, one by one
61          String[] resourceIds = this.collection.listResources();
62          ResourceSet resources = new GenericResourceSet();
63          for (int i = 0; i < resourceIds.length; i++){
64              try {
65                  Resource resource = (Resource) this.collection.getResource(resourceIds[i]);
66                  if ("XMLResource".equals(resource.getResourceType())){
67                      XMLResource xResource = (XMLResource) resource;
68                      Node node = xResource.getContentAsDOM();
69                      NodeList nodeList = XPathAPI.selectNodeList(node,query);
70                      for (int j = 0, sz = nodeList.getLength(); j < sz; j++){
71                          Node resultNode = nodeList.item(j);
72                          //if the result node is the same as the resource node,
73                          //then we can simply add the resource
74                          if (resultNode.equals(node)){
75                              resources.addResource(xResource);
76                          }
77                          //otherwise we have to build a new resource
78                          else {
79                              GenericXmlNodeResource nodeResource =
80                                      new GenericXmlNodeResource(resultNode);
81                              resources.addResource(nodeResource);
82                          }
83                      }
84                  }
85              } catch (XMLDBException e){
86                  log.warn("Error:",e);
87              } catch (TransformerException e){
88                  log.warn("Error while doing xpath query",e);
89              }
90          }
91          return resources;
92      }
93      
94      /***Unsupported
95       *@todo Figure out how namespaces fit in and implement this method
96       */
97      public String getNamespace(String str) {
98          throw new UnsupportedOperationException();
99      }
100     
101     /***Sets the collection in which to do queries against
102      *@param collection The collection to do a query against
103      */
104     public void setCollection(Collection collection) {
105         if (this.collection != null){
106             throw new IllegalStateException("Collection property has " +
107                     "already been set and can not be changed.");
108         }
109         this.collection = collection;
110     }
111     
112     /***Unsupported
113      *@todo Figure out how namespaces fit in and implement this method
114      */
115     public void setNamespace(String str, String str1) {
116         throw new UnsupportedOperationException();
117     }
118     
119     /*** Unsupported
120      *@todo Implement this method
121      */
122     public ResourceSet queryResource(String str, String str1) {
123         throw new UnsupportedOperationException();
124     }
125     
126     /***Returns the version, currently 1.0
127      *@return "1.0"
128      */
129     public String getVersion() {
130         return this.version;
131     }
132     
133     /*** Returns XPathQueryService, which is the name of this service
134      *@return 'XPathQueryService'
135      */
136     public String getName() {
137         return "XPathQueryService";
138     }
139     
140     /***Unsupported
141      *@todo Figure out how namespaces fit in and implement this method
142      */
143     public void clearNamespaces() {
144         throw new UnsupportedOperationException();
145     }
146     
147 }